package com.huarui.roleTemplate.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.ewaf.framework.action.BaseAction;
import org.ewaf.framework.model.ListModel;

import com.huarui.roleTemplate.model.RoleTemplate;
import com.huarui.roleTemplate.service.RoleTemplateService;
import com.huarui.user.model.SessionMessage;
import com.huarui.user.security.UserHolder;
import com.opensymphony.xwork2.Action;

@SuppressWarnings("serial")
public class RoleTemplateAction extends BaseAction {
	@SuppressWarnings("unchecked")
	private ListModel listModel=new ListModel();
	private Map<String, Object> resultMap = new HashMap<String, Object>();
	@SuppressWarnings("unused")
	private SessionMessage sm;
	private List<Map> list;
	private Integer start;
	private Integer limit;
	private Integer roleTemplateId;
	private String roleTemplateJson;
	private String checkedPreTree;
	private String unCheckedPreTree;
	private Integer id;
	private RoleTemplateService roleTemplateService;
	public Map<String, Object> getResultMap() {
		return resultMap;
	}
	
	@SuppressWarnings("unchecked")
	public String extList(){
		sm=UserHolder.getCurrentSessionMessage();
		listModel.setPage(start == null ? 0 : start / limit + 1);
		listModel.setPageSize(limit == null ? 10 : limit);
		Map<String, Object> map = new HashMap<String, Object>();
		List<Map> list=roleTemplateService.pagingByCondition(listModel,map);
		resultMap.put("root", list);
		resultMap.put("totalProperty", listModel.getTotal());
		return Action.SUCCESS;
	}
	@SuppressWarnings("unchecked")
	public String getRoleTemplatePermissionList(){
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("roleTemplateId", roleTemplateId);
		list=roleTemplateService.getRoleTemPermByCondition(map);
		return "list";
	}
	
	public String save(){
		RoleTemplate roleTemplate=(RoleTemplate) JSONObject.toBean(JSONObject.fromObject(roleTemplateJson),RoleTemplate.class);
		List<Integer> checkedPreList=(List<Integer>) JSONArray.toCollection(JSONArray.fromObject(checkedPreTree),Integer.class);
		List<Integer> unCheckedPreList=(List<Integer>) JSONArray.toCollection(JSONArray.fromObject(unCheckedPreTree),Integer.class);
		resultMap=roleTemplateService.save(roleTemplate, checkedPreList, unCheckedPreList);
		return Action.SUCCESS;
	}
	public String getRoleTemplateById(){
		resultMap=roleTemplateService.getRoleTemplateById(id);
		return Action.SUCCESS;
	}
	public List<Map> getList() {
		return list;
	}
	public void setListModel(ListModel listModel) {
		this.listModel = listModel;
	}
	public void setSm(SessionMessage sm) {
		this.sm = sm;
	}
	public void setStart(Integer start) {
		this.start = start;
	}
	public void setLimit(Integer limit) {
		this.limit = limit;
	}

	public void setRoleTemplateService(RoleTemplateService roleTemplateService) {
		this.roleTemplateService = roleTemplateService;
	}

	public void setRoleTemplateId(Integer roleTemplateId) {
		this.roleTemplateId = roleTemplateId;
	}

	public void setRoleTemplateJson(String roleTemplateJson) {
		this.roleTemplateJson = roleTemplateJson;
	}

	public void setCheckedPreTree(String checkedPreTree) {
		this.checkedPreTree = checkedPreTree;
	}

	public void setUnCheckedPreTree(String unCheckedPreTree) {
		this.unCheckedPreTree = unCheckedPreTree;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	
}
